﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PicSync.Backend
{
    //TODO : Perhaps change name. Kinda sucks
    //TODO : Add method that constantly checks for images being changed / deleted / added
    //TODO : Add a class that deals with saying and loading information, such as AccessToken and folder to monitor

    /// <summary>
    /// This class monitors the selected folder with all the users pictures
    /// </summary>

    class MonitorFolder
    {
        string dirPath;
        FileSystemWatcher watcher = new System.IO.FileSystemWatcher(); 
        public MonitorFolder(String dirPath)
        {
            this.dirPath = dirPath;
            watcher.Path = dirPath;
            watcher.IncludeSubdirectories = true;
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);
            watcher.EnableRaisingEvents = true;
        }
        void OnChanged(object sender, FileSystemEventArgs e)
        {
            if (e.ChangeType == WatcherChangeTypes.Deleted)
            {
                //TODO : Add deleted code ; delete picture on Facebook
                Console.WriteLine("File " + e.Name + " deleted");
            }
            if (e.ChangeType == WatcherChangeTypes.Created)
            {
                //TODO : Add created code ; upload picture to Facebook
                Console.WriteLine("File " + e.Name + " created");
                Console.WriteLine("Do you wish to post this to facebook?");
                Console.WriteLine("");
                Console.WriteLine(e.FullPath);
                String post = Console.ReadLine();
                if (post.Trim().ToLower().Equals("true"))
                {
                    TESTS.TEST_BackEnd_UploadPictures.uploadPicture(e.FullPath);
                }

                
            }
            if (e.ChangeType == WatcherChangeTypes.Changed)
            {
                //TODO : Update changed code ; upload updated picture to Facebook
                Console.WriteLine("File " + e.Name + " changed");
            }
            if (e.ChangeType == WatcherChangeTypes.Renamed)
            {
                //TODO : Update picture name on Facebook
                Console.WriteLine("File " + e.Name + " renamed " + e.ChangeType);
            }
        }
        void OnRenamed(object sender, RenamedEventArgs e)
        {
            //TODO : Update picture name on Facebook. Find old picture and change name
            Console.WriteLine("File " + e.OldName + " renamed to" + e.Name);
        }

    }
}
